//这个数据是用来添加（修改）管理员时，设定权限
//1、定义全部的菜单数据

export const menudata = [
    {
        id: "1",
        label: "首页",
        path: "/Admin/Home"
    },
    {
        id: "2",
        label: "商品",
        children: [
            {
                id: "2-1",
                label: "商品列表",
                path: "/Admin/QueryList"
            },
            {
                id: "2-2",
                label: "商品分页",
                path: "/Admin/ListWithPage"
            }

        ]
    },
    {
        id: "3",
        label: "轮播图管理",
        children: [
            {
                id:"3-1",
                label:"轮播图列表",
                path:"/Admin/BannerList",
                children:[
                    {
                        id:"3-1-R",
                        label:"轮播图列表显示"
                    },
                    {
                        id:"3-1-U",
                        label:"修改轮播图状态"
                    },
                    {
                        id:"3-1-D",
                        label:"删除轮播图"
                    },
                ]
            },
            {
                id:"3-2",
                label:"轮播图添加",
                path:"/Admin/BannerAdd"
            }
        ]
    },
    {
        id:"4",
        label:"操作人员管理",
        children:[
            {
                id:"4-1",
                label:"操作人员列表",
                path:"/Admin/AdminList",
                children:[
                    {
                        id:"4-1-R",
                        label:"显示操作人员"
                    },
                    {
                        id:"4-1-D",
                        label:"删除操作人员"
                    },
                    {
                        id:"4-1-U",
                        label:"修改操作人员"
                    }
                ]
            },
            {
                id:"4-2",
                label:"添加操作人员",
                path:"/Admin/AdminAdd"
            }
        ]
    },
    {
        id:"5",
        label:"购物车管理",
        children:[
            {
                id:"5-1",
                label:"购物车列表",
                path:"/Admin/ShoppingCar"
            }
        ]
    },
    {
        id:"6",
        label:"订单管理",
        children:[
            {
                id:"6-1",
                label:"订单列表",
                path:"/Admin/Order"
            }
        ]
    },
    {
        id:"7",
        label:"用户管理",
        children:[
            {
                id:"7-1",
                label:"用户列表",
                path:"/Admin/User"
            }
        ]
    }
];

//2、根据当前用户的权限，显示响应的菜单
export default function getMenus () {
    //1)、获取该用户的权限（登录时已经保存过了）
    let checkedkeys:string[] = []
    let currKeys = sessionStorage.getItem("grants")
    if(currKeys){
        checkedkeys = JSON.parse(currKeys)
    }
    if( checkedkeys.length === 0 ){
        checkedkeys = ['1','2','2-1','2-2','3','3-1','3-2','4','4-1','4-2','5','5-1','6','6-1','7','7-1']
    }

    //2）、根据用户的权限结合全部菜单数据，产生该用户的菜单

    interface IMenuItem{
        id:string,
        label:string,
        path?:string,
        children?:IMenuItem[]
    }

    let currMenuData:IMenuItem[] = []//存储当前用户的菜单项

    menudata.forEach(item=>{
        if(checkedkeys.indexOf(item.id)>-1){
            //定义一个菜单项
            const newmenuItem = {...item};
            //处理子一级
            if(item.children){
                newmenuItem.children = []
                item.children.forEach(sonItem=>{
                    if(checkedkeys.indexOf(sonItem.id)>-1){
                        newmenuItem.children?.push({...sonItem})
                    }
                })
            }
            currMenuData.push(newmenuItem)
        }
    })

    console.log("currMenuData",currMenuData);
    return currMenuData
}